/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.components.filebrowser;

import br.com.paulocanedo.pc9.PSimpleBorder;
import br.com.paulocanedo.third.effects.GraphicsUtilities;
import br.com.pc9.pswing.components.SearchComponent;
import br.com.pc9.pswing.util.SystemUtilsOS;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.ListSelectionModel;

/**
 *
 * @author paulocanedo
 */
public class PFileBrowserView {

    private JComponent masterComponent = new JPanel(new BorderLayout());
    private JComponent topPanel;
    private JComponent leftPanel;
    private JComponent bottomPanel;
    private JComponent rightPanel;
    private JComponent centerPanel;
    private Map<String, Object> components = new HashMap<String, Object>();

    public PFileBrowserView() {
        init();

        initDimensions();
    }

    public JComponent getMasterComponent() {
        return masterComponent;
    }

    private void init() {
        masterComponent.add(topPanel = createTopComponent(), BorderLayout.NORTH);
        masterComponent.add(leftPanel = createLeftComponent(), BorderLayout.WEST);
        masterComponent.add(bottomPanel = createBottomComponent(), BorderLayout.SOUTH);
        masterComponent.add(centerPanel = createCenterComponent(), BorderLayout.CENTER);

//        masterComponent.add(rightPanel = createRightComponent(), BorderLayout.EAST);
    }

    private void initDimensions() {
        int verticalBarValue = 44;
        int horizontalBarValue = 190;

        topPanel.setPreferredSize(new Dimension(masterComponent.getWidth(), verticalBarValue));
        leftPanel.setPreferredSize(new Dimension(horizontalBarValue, masterComponent.getHeight() - verticalBarValue));
        bottomPanel.setPreferredSize(new Dimension(masterComponent.getWidth(), verticalBarValue));
//        rightPanel.setPreferredSize(new Dimension(horizontalBarValue, masterComponent.getHeight() - verticalBarValue));
    }

    protected JComponent createTopComponent() {
        JComponent aTopPanel = new JPanel();
        aTopPanel.setLayout(new FlowLayout());
        aTopPanel.setBorder(new PSimpleBorder(new Insets(2, 4, 2, 4), Color.GRAY, PSimpleBorder.PaintInPosition.BOTTOM));
        int controlCorners = 5;
        int spaceSeparator = 4;

        JButton backButton = createButton("<", "first", controlCorners);
        backButton.setToolTipText("Ver pastas que você navegou recentemente");
        JButton forwardButton = createButton(">", "last", controlCorners);
        forwardButton.setToolTipText("Ver pastas que você navegou recentemente");

        JButton gotoButton = createButton("Ir", "first", controlCorners);
        gotoButton.setToolTipText("Informar manualmente diretório desejado");
        JToggleButton previewButton = createToggleButton("P", "middle", controlCorners);
        JToggleButton showHiddenButton = createToggleButton("", "last", controlCorners);
        Dimension preferredSize = showHiddenButton.getPreferredSize();
        showHiddenButton.setIcon(createEyeIcon(preferredSize.width, preferredSize.height));

        JComboBox navigationFoldersComboBox = new JComboBox();
        navigationFoldersComboBox.setPreferredSize(new Dimension(235, (int) backButton.getPreferredSize().getHeight()));

        SearchComponent searchComponent = new SearchComponent();
        JComponent searchField = searchComponent.getComponent();
        searchField.setPreferredSize(new Dimension(200, (int) backButton.getPreferredSize().getHeight() + 8));

        JPanel controlContainer = new JPanel(new GridLayout());
        controlContainer.add(backButton);
        controlContainer.add(forwardButton);
        controlContainer.add(newSeparator(4));
        controlContainer.add(gotoButton);
//        controlContainer.add(previewButton);
        controlContainer.add(showHiddenButton);

        aTopPanel.add(controlContainer);
        aTopPanel.add(newSeparator(spaceSeparator));
        aTopPanel.add(navigationFoldersComboBox);
        aTopPanel.add(newSeparator(spaceSeparator));
        aTopPanel.add(searchField);

        components.put(BACK_BUTTON, backButton);
        components.put(FORWARD_BUTTON, forwardButton);
        components.put(GOTO_BUTTON, gotoButton);
        components.put(PREVIEW_BUTTON, previewButton);
        components.put(SHOW_HIDDEN_BUTTON, showHiddenButton);
        components.put(NAVIGATION_FOLDERS_COMBOBOX, navigationFoldersComboBox);
        components.put(SEARCH_COMPONENT, searchComponent);

        return aTopPanel;
    }

    protected JComponent createLeftComponent() {
        JComponent aLeftPanel = new JPanel();
        aLeftPanel.setLayout(new GridLayout());
        aLeftPanel.setBackground(Color.WHITE);
        JList list = new JList();
        list.setCellRenderer(new PFileListCellRenderer());
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(list);
        list.setBorder(new PSimpleBorder(new Insets(0, 0, 0, 0), Color.GRAY, PSimpleBorder.PaintInPosition.RIGHT));
        scrollPane.setBorder(BorderFactory.createEmptyBorder());

        aLeftPanel.add(scrollPane);
        components.put(FAVORITE_LIST, list);

        return aLeftPanel;
    }

    protected JComponent createBottomComponent() {
        JComponent aBottomPanel = new JPanel();
        aBottomPanel.setBorder(new PSimpleBorder(new Insets(1, 1, 1, 1), Color.GRAY, PSimpleBorder.PaintInPosition.TOP));
        aBottomPanel.setLayout(new BorderLayout());

        JButton newFolderButton = new JButton("Nova pasta");
        JButton cancelButton = new JButton("Cancelar");
        JButton confirmButton = new JButton("Abrir");

        JPanel leftContainer = new JPanel();
        leftContainer.add(newFolderButton);

        JPanel rightContainer = new JPanel();
        rightContainer.add(SystemUtilsOS.isWindows() ? confirmButton : cancelButton);
        rightContainer.add(SystemUtilsOS.isWindows() ? cancelButton : confirmButton);

        aBottomPanel.add(leftContainer, BorderLayout.WEST);
        aBottomPanel.add(rightContainer, BorderLayout.EAST);

        components.put(NEW_FOLDER_BUTTON, newFolderButton);
        components.put(CANCEL_BUTTON, cancelButton);
        components.put(CONFIRM_BUTTON, confirmButton);

        return aBottomPanel;
    }

    protected JComponent createRightComponent() {
        JComponent aRightPanel = new JPanel();
        aRightPanel.setBorder(new PSimpleBorder(new Insets(0, 0, 0, 0), Color.GRAY, PSimpleBorder.PaintInPosition.LEFT));
        return aRightPanel;
    }

    protected JComponent createCenterComponent() {
        JComponent aCenterPanel = new JPanel();
        aCenterPanel.setBackground(Color.WHITE);
        aCenterPanel.setLayout(new BoxLayout(aCenterPanel, BoxLayout.LINE_AXIS));

        JList browserList = new JList();
        browserList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        browserList.setCellRenderer(new PFileListCellRenderer());

        JScrollPane scrollPane = new JScrollPane(browserList);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());

        aCenterPanel.add(scrollPane);
        components.put("browserList", browserList);

        return aCenterPanel;
    }

    protected Object getComponent(String name) {
        return components.get(name);
    }

    protected AbstractButton getButton(String name) {
        return (AbstractButton) getComponent(name);
    }

    protected JList getJList(String name) {
        return (JList) getComponent(name);
    }

    private void setButtonProperties(AbstractButton button, String segmentPosition, int roundCorner) {
        button.putClientProperty("JButton.segmentPosition", segmentPosition);
        button.putClientProperty("maxRoundCorner", roundCorner);
    }

    private JButton createButton(String text, String position, int corner) {
        JButton button = new JButton(text);

        button.setPreferredSize(new Dimension(32, 24));
        setButtonProperties(button, position, corner);
        return button;
    }

    private JToggleButton createToggleButton(String text, String position, int corner) {
        JToggleButton button = new JToggleButton(text);
        button.setPreferredSize(new Dimension(32, 24));
        setButtonProperties(button, position, corner);
        return button;
    }

    private JComponent newSeparator(int space) {
        JComponent separatorPanel = new JPanel();
        separatorPanel.setPreferredSize(new Dimension(space, space));
        return separatorPanel;
    }

    private Icon createEyeIcon(int w, int h) {
        BufferedImage image = GraphicsUtilities.createCompatibleTranslucentImage(w, h);

        Graphics2D g2 = (Graphics2D) image.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(new Color(0xdcdcea));

        int xspace = 4, yspace = 0;

        int[] x = new int[]{xspace, w / 2, w - xspace, w / 2};
        int[] y = new int[]{h / 2, yspace, h / 2, h - yspace};
        GeneralPath path = new GeneralPath();
        path.moveTo(x[0], y[0]);
        path.curveTo(x[0], y[0], x[1], y[1], x[2], y[2]);
        path.curveTo(x[2], y[2], x[3], y[3], x[0], y[0]);
        path.closePath();
        g2.fill(path);
        g2.setColor(Color.GRAY);
        g2.draw(path);

        g2.setColor(Color.GRAY);
        int sizeCircle = h / 3;
        g2.translate(-(sizeCircle / 2), -((h - sizeCircle) / 2));
        g2.fillOval(w / 2, w / 2, sizeCircle, sizeCircle);

        g2.dispose();
        return new ImageIcon(image);
    }
    protected final static String BACK_BUTTON = "backButton";
    protected final static String FORWARD_BUTTON = "forwardButton";
    protected final static String GOTO_BUTTON = "gotoButton";
    protected final static String PREVIEW_BUTTON = "previewButton";
    protected final static String SHOW_HIDDEN_BUTTON = "showHiddenButton";
    protected final static String NAVIGATION_FOLDERS_COMBOBOX = "navigationList";
    protected final static String SEARCH_COMPONENT = "searchField";
    protected final static String FAVORITE_LIST = "favoriteList";
    protected final static String BROWSER_LIST = "browserList";
    protected final static String NEW_FOLDER_BUTTON = "newFolderButton";
    protected final static String CANCEL_BUTTON = "cancelButton";
    protected final static String CONFIRM_BUTTON = "confirmButton";
}
